package org.iryshka.skyserver;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.iryshka.skyserver.ConfigAnalyze.Listing;

public class SkyResponse {
	
	static Date date = new Date();
	static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy   HH:mm");
	
	public SkyRequest skyRQST;
	public long TSpace;
	
	public SkyResponse (SkyRequest skyRQST){
		this.skyRQST = skyRQST;
	}
	
	private static String getTime () {
		return dateFormat.format(date);		
	}
	
	public String buildResponseHeader(String strCase){
		switch (strCase){
			case("200"):
				return "HTTP/1.1 200 OK\r\n" +
		                "Content-type: " + skyRQST.hashMapAnalyze() + "\r\n" +
		                "Content-length:" + this.TSpace + "\r\n" +
		                "Connection: close" + "\r\n\r\n";
			case("400"):
				return "HTTP/1.1 400 Bad Request\r\n" + 
						"Content-type: text/html; charset=utf-8\r\n" + 
						"Connection: close" + "\r\n\r\n" +
						"<html><head><title>Bad Request</title></head><body>The request cannot be"+
						" fulfilled due to bad syntax.</body></html>";
			case("403"):
				return "HTTP/1.1 403 Forbidden\r\n" + 
						"Content-type: text/html; charset=utf-8\r\n" + 
						"Connection: close" + "\r\n\r\n" +
						"<html><head><title>Forbidden</title></head><body>The request cannot be"+
						" fulfilled by server.</body></html>";
			case("404"):
				return "HTTP/1.1 404 Error\r\n" + 
						"Content-type: text/html; charset=utf-8\r\n" + 
						"Connection: close" + "\r\n\r\n" +
						"<html><head><title>NOT FOUND</title></head><body>The resource you are looking" +
						" for has been removed, had its name changed, or is temporarily unavailable.</body></html>";
			case("405"):
				return "HTTP/1.1 405 Method Not Allowed\r\n" + 
						"Content-type: text/html; charset=utf-8\r\n" + 
						"Connection: close" + "\r\n\r\n" +
						"<html><head><title>Method Not Allowed</title></head><body>A request was made of a resource " +
						"using a request method not supported by that resource.</body></html>";
			case("429"):
				return "HTTP/1.1 429 Too Many Requests\r\n" + 
						"Content-type: text/html; charset=utf-8\r\n" + 
						"Connection: close" + "\r\n\r\n" +
						"<html><head><title>Too Many Requests</title></head><body>Too many requests in a given" +
						" amount of time.";
			case("List"):
				return "HTTP/1.1 200 OK\r\n" +
						"Content-type: text/html; charset=utf-8\r\n" +
		                "Connection: close" + "\r\n\r\n";
		}
		return null;
	}
	
	public String formHtmlList (File requestedDir){
		if(ConfigAnalyze.ListingOnOff == Listing.OFF ){
			buildResponseHeader("403");
			return null;
		}
		
		StringBuilder formedListResponse = new StringBuilder(500);
		formedListResponse = formedListResponse.append(buildResponseHeader("List"));
		formedListResponse = formedListResponse.append((
				"<html><head><title>HOSTNAME /"+ requestedDir.getName() +"</title>" +
				"<link type=\"text/css\" rel=\"stylesheet\" href=\"helpCss.css\"></head>" + 
				"<body>"));
		formedListResponse = formedListResponse.append((
				"<h2> hostname /" + requestedDir.getName() + "/</h2>" +
				"<hr><pre >"+"<a href=\"/\">[To Parent Directory]</a><br>"));
			
		File [] filesInDir = requestedDir.listFiles();
		for(File F : filesInDir){
			formedListResponse.append("<br>");
			formedListResponse.append(getTime());
			formedListResponse.append("  " + F.getTotalSpace() + "  ");
			formedListResponse.append("<a href=\""+ formHref(requestedDir.getName(), F.getName()) + "\">" + F.getName() + "</a>");
		}
		formedListResponse = formedListResponse.append("<br></pre><hr></body></html>");
		return new String(formedListResponse);
	}
	
	private String formHref (String folderName, String fileName){
		return "/" + folderName + "/" + fileName.replace(" ", "%");
	}
}